var express = require("express");
var router = express.Router();
let jwt = require("jsonwebtoken");
let leaveService = require("../services/leaveServie");

/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", { title: "Express" });
});
//更新接口
router.patch("/update", (req, res, next) => {
  let token = req.headers.token;
  jwt.verify(token, "2306", async (err, info) => {
    if (!err && info.usertype == 1) {
      //表示老师
      let { id, type } = req.body;
      console.log(id, type);
      let result = await leaveService.leaveUpdate(
        { _id: id },
        {
          $set: { type },
        }
      );
      if (result.nModified) {
        res.json({
          code: 200,
          msg: "更新成功",
        });
      } else {
        res.json({
          code: 203,
          msg: "更新失败",
        });
      }
    } else {
      res.json({
        code: 203,
        msg: "token异常或者权限不足",
      });
    }
  });
});
//列表  学生查看自己的请假记录
router.get("/list", (req, res, next) => {
  let token = req.headers.token;
  jwt.verify(token, "2306", async (err, info) => {
    if (!err && info.usertype == 2) {
      //表示学生
      let userid = info.userid;
      let result = await leaveService.leaveQuery({
        userid,
      });
      res.json({
        code: 200,
        msg: "查询成功",
        data: result,
      });
    } else {
      res.json({
        code: 203,
        token: "token异常或者权限不够",
      });
    }
  });
});
router.get("/group", async (req, res, next) => {
  let key = req.query.key;
  let  result  = await  leaveService.leaveGroup(key);
  res.json({
    code:200,
    msg:"分组",
    data:result
  })

});

//所有的列表  老师查看所有请假记录
router.get("/listall", (req, res, next) => {
  let token = req.headers.token;
  jwt.verify(token, "2306", async (err, info) => {
    if (!err && info.usertype == 1) {
      //表示老师
      let { shownum, pageindex, key, type } = req.query; //pageindex表示第几页
      //shownum 表示每页显示多少数据
      let params = {};
      if (key) {
        params.username = new RegExp(key);
      }
      if (type) {
        params.type = type;
      }

      let result = await leaveService.leaveQueryAll(
        params,
        (pageindex - 1) * shownum,
        shownum * 1
      );
      let count = await leaveService.leaveQueryAllCount(params);
      res.json({
        code: 200,
        msg: "查询成功",
        data: result,
        total: count,
      });
    } else {
      res.json({
        code: 203,
        token: "token异常或者权限不够",
      });
    }
  });
});

router.post("/add", (req, res, next) => {
  let token = req.headers.token;

  jwt.verify(token, "2306", async (err, info) => {
    if (!err && info.usertype == 2) {
      //没有错 并且是学生
      let { reason, start, end, extend, type = 1 } = req.body; //req.query
      let { userid, username } = info;

      let result = await leaveService.leaveAdd({
        userid,
        username,
        reason,
        start,
        end,
        extend,
        type,
      });

      res.json({
        code: 200,
        msg: "新增成功",
        data: result,
      });
    } else {
      res.json({
        code: 203,
        msg: "token异常或者权限不够",
      });
    }
  });
});

module.exports = router;
